import {getUserInfo} from '../../api/member.js';
import {getTaskContent, taskZan, getGcStat} from "../../api/activity";

import Util from '../../utils/util.js';

const app = getApp();
Page({
  data: {
    loading: false, // 是否加载中
    loadend: false, // 是否加载完毕
    loadTitle: '加载更多内容', // 提示语
    lists: [],
    page: 1,
    et_show: true,

    tabNavs: [
      {title: "全部", value: 0},
      {title: "任务", value: 1},
      {title: "特殊积分", value: 2},
      {title: "随想", value: 3},
    ],
    current_index: 0,

    gc_amount: 0,//广场数量
    original: 'message',
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    const that = this;

    Util.chekWxLogin().then(res => {
      getUserInfo().then(result => {
        that.setData({
          member: result.data || {}
        })
        if (Util.isEmpty(result.data.avatar)) {
          wx.navigateTo({
            url: "../member/edit"
          });
          return;
        }

        wx.showLoading({
          title: '正在加载',
        })
        getGcStat().then(res => {
          wx.hideLoading();
          that.setData({
            gc_amount: res.data.gc_amount || 0
          })
        }).catch(errmsg => {
          wx.hideLoading();
        })
        that.getLists()
      }).catch(errmsg => {
        app.Tips({title: errmsg})
      })
    })
    // this.onLoadTabbar();
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    const is_refresh = wx.getStorageSync('is_refresh')
    if (is_refresh) {
      wx.removeStorageSync('is_refresh')
      this.setData({
        loading: false,
        loadend: false,
        loadTitle: '加载更多内容',
        lists: [],
        page: 1,
      })
      this.getLists();
    }
  },

  wfZan(e) {
    const that = this,
      key = e.currentTarget.dataset.k

    let lists = this.data.lists || null
    if (Util.isEmpty(lists))
      return;

    taskZan(lists[key].id).then(res => {
      if (res.data.status == 1) {
        lists[key]['is_zan'] = 1;
        lists[key]['star_amount'] = lists[key]['star_amount'] + 1;
      } else {
        lists[key]['is_zan'] = 0;
        lists[key]['star_amount'] = lists[key]['star_amount'] - 1;
      }
      that.setData({lists: lists})
    })

  },

  getLists() {
    const that = this
    if (that.data.loadend || that.data.loading) {
      return;
    }
    let filter = that.data.filter || {}
    filter['page_num'] = that.data.page;

    if (this.data.tabNavs[this.data.current_index].value) {
      filter['type'] = this.data.tabNavs[this.data.current_index].value
    }

    that.setData({loading: true, loadTitle: '正在加载...'});
    let lists = that.data.lists || []
    getTaskContent(filter).then(res => {
      const list = res.data.items || []
      const loadend = res.data.last_page <= that.data.page;

      lists = app.SplitArray(list, lists);
      that.setData({
        lists: lists,
        loadend: loadend,
        loading: false,
        loadTitle: loadend ? '--没有更多了--' : '正在加载...',
        total: res.data.total,
        page: that.data.page + 1,
        et_show: lists.length > 0
      });
      wx.stopPullDownRefresh()
    }).catch(errmsg => {
      wx.stopPullDownRefresh()
      app.Tips({title: errmsg})
    })
  },

  previewMutiImage(e) {

    const that = this,
      index = e.currentTarget.dataset.index,
      key = e.currentTarget.dataset.key,
      field = e.currentTarget.dataset.field,
      lists = this.data.lists || null

    if (Util.isEmpty(lists))
      return;

    let urls = [], current_src = ""
    if (!Util.isEmpty(field) && !Util.isEmpty(lists) && !Util.isEmpty(lists[index].extends[field].values)) { //1
      for (const k in lists[index].extends[field].values) {
        urls.push(Util.previewCdnImage(lists[index].extends[field].values[k].path));
      }
      current_src = Util.previewCdnImage(lists[index].extends[field].values[key].path);
    } else if (!Util.isEmpty(lists[index].pictures)) {
      for (const k in lists[index].pictures) {
        urls.push(Util.previewCdnImage(lists[index].pictures[k].path));
      }
      current_src = Util.previewCdnImage(lists[index].pictures[key].path)
    }

    if (Util.isEmpty(urls))
      return;

    wx.previewImage({
      current: current_src,
      urls: urls
    });
  },

  changeTab(e) {
    if (this.data.current_index == e.currentTarget.dataset.v)
      return

    this.setData({
      current_index: e.currentTarget.dataset.v,
      loading: false, // 是否加载中
      loadend: false, // 是否加载完毕
      loadTitle: '加载更多内容', // 提示语
      lists: [],
      page: 1,
    })

    this.getLists();
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {
  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {
    this.setData({
      loading: false,
      loadend: false,
      loadTitle: '加载更多内容',
      lists: [],
      page: 1,
    })
    this.getLists();
  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
    this.getLists();
  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }

})